//SharePoint Video Player
//By Alon Havivi(havivi@gmail.com, http://havivi.blogspot.com/) 
//For full source code and Terms Of Use,
//visit http://svp.codeplex.com/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

namespace Havivi.SharePointVideoPlayer
{
    [Guid("1c3f0096-e095-494e-a013-20e259c583ec")]
    public class SVP : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool error = false;       
        string scriptName = "HaviviPlayerScript";
        


        private const string EmbeddedScript = @"<script type=""text/javascript"" src=""/_layouts/SCRIPTS/SharePointVideoPlayer/flowplayer-3.1.4.min.js""></script>
                             <link rel=""stylesheet"" type=""text/css"" href=""/_layouts/SCRIPTS/SharePointVideoPlayer/style.css"">";

        private const string EmbeddedPlayer = @"<a   href=""{0}""   style=""display:block;width:{1}px;height:{2}px"" class=""myPlayer""></a>" +
                @"<script>flowplayer(""a.myPlayer"", ""/_layouts/SCRIPTS/SharePointVideoPlayer/flowplayer-3.1.1.swf"");</script>";



        private string flvBestand = null;
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Video Player Properties")]
        [WebDisplayName("FLV file")]
        [WebDescription("FLV file")]
        public string FLVBestand
        {
            get
            {
                if (flvBestand == null)
                {
                    flvBestand = "http://e1p1.simplecdn.net/flowplayer/flowplayer-700.flv";
                }
                return flvBestand;
            }
            set
            {
                flvBestand = value;
            }
        }

        private string playerWidth = null;
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Video Player Properties")]
        [WebDisplayName("Player width")]
        [WebDescription("Player width")]
        public string PlayerWidth
        {
            get
            {
                if (playerWidth == null)
                {
                    playerWidth = "520";
                }
                return playerWidth;
            }
            set
            {
                playerWidth = value;
            }
        }

        private string playerHeight = null;
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Video Player Properties")]
        [WebDisplayName("Player height")]
        [WebDescription("Player height")]
        public string PlayerHeight
        {
            get
            {
                if (playerHeight == null)
                {
                    playerHeight = "330";
                }
                return playerHeight;
            }
            set
            {
                playerHeight = value;
            }
        }



        public SVP()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!error)
            {
                try
                {

                    base.CreateChildControls();
                    this.ChromeType = PartChromeType.None;
                    Table table = new Table();
                    table.Width = Unit.Percentage(100);
                    table.CellPadding = 0;
                    table.BorderWidth = 0;
                    table.CellSpacing = 0;
                    TableRow row = new TableRow();
                    row.Width = Unit.Percentage(100);
                    TableCell cell = new TableCell();
                    cell.Width = Unit.Percentage(100);
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    cell.VerticalAlign = VerticalAlign.Middle;
                    Label literalControl = null;
                    literalControl = new Label();
                    literalControl.Text = string.Format(EmbeddedPlayer, FLVBestand, PlayerWidth, PlayerHeight);
                    cell.Controls.Add(literalControl);
                    row.Controls.Add(cell);
                    table.Controls.Add(row);
                    this.Controls.Add(table);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!error)
            {
                try
                {
                    base.OnLoad(e);
                    
                     
                    if (!this.Page.IsClientScriptBlockRegistered(scriptName))
                    {
                        this.Page.RegisterClientScriptBlock(scriptName, EmbeddedScript);
                    }

                    this.EnsureChildControls();


                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this.error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
